﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BuildPlannerLib.Enums;
using BuildPlannerLib.SimEvents;
using BuildPlannerLib.Terran.Buildings;

namespace BuildPlannerLib.Terran.SimEvents
{
    // Derived class from Event for a MULE being called down
    public class MULECalldown : SimEvent
    {
        // Field for the Command Center
        public IndividualCommandCenter CommandCenter;

        // Basic Constructor with parameters for parent build, Command Center and time
        public MULECalldown(Build build, IndividualCommandCenter commandCenter, int time)
        {
            Build = build;
            Time = time;
            CommandCenter = commandCenter;
        }

        public override void Execute()
        {
            // "if" statement allows actions to use the energy first
            if (CommandCenter.Energy.Amount >= 50)
            {
                Base MULEBase;
                MineralFlags mineralFlags;
                int newTime = TimeConvert.ToInt(Build.CurrentTime);
                // Uses close mineral patches first and adds all events for MULE mineral returns
                if (Build.Map.Bases.Exists(delegate(Base b) { return b.MULEIdleMineralPatches[2] > 0; }))
                {
                    MULEBase = Build.Map.Bases.Find(delegate(Base b) { return b.MULEIdleMineralPatches[2] > 0; });
                    MULEBase.MULEIdleMineralPatches[2]--;
                    mineralFlags = MineralFlags.ClosePatch;
                    newTime += 789;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                }
                else if (Build.Map.Bases.Exists(delegate(Base b) { return b.MULEIdleMineralPatches[3] > 0; }))
                {
                    MULEBase = Build.Map.Bases.Find(delegate(Base b) { return b.MULEIdleMineralPatches[3] > 0; });
                    MULEBase.MULEIdleMineralPatches[3]--;
                    mineralFlags = MineralFlags.ClosePatch | MineralFlags.GoldPatch;
                    newTime += 789;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 975;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                }
                else if (Build.Map.Bases.Exists(delegate(Base b) { return b.MULEIdleMineralPatches[0] > 0; }))
                {
                    MULEBase = Build.Map.Bases.Find(delegate(Base b) { return b.MULEIdleMineralPatches[0] > 0; });
                    MULEBase.MULEIdleMineralPatches[0]--;
                    mineralFlags = MineralFlags.FarPatch;
                    newTime += 841;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                }
                else if (Build.Map.Bases.Exists(delegate(Base b) { return b.MULEIdleMineralPatches[1] > 0; }))
                {
                    MULEBase = Build.Map.Bases.Find(delegate(Base b) { return b.MULEIdleMineralPatches[1] > 0; });
                    MULEBase.MULEIdleMineralPatches[1]--;
                    mineralFlags = MineralFlags.GoldPatch;
                    newTime += 841;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                }
                else
                {
                    newTime += 841;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    newTime += 1079;
                    Build.EventList.Add(new MULEReturnMinerals(Build, newTime));
                    return;
                }
                // Adds an event to free up the mineral patch for future MULEs after this one despawns
                newTime = TimeConvert.ToInt(Build.CurrentTime) + 9000;
                Build.EventList.Add(new MULEDespawn(Build, MULEBase, mineralFlags, newTime));
            }
        }
    }
}
